package com.cheng.shiro;

import com.cheng.common.exception.CommonException;
import com.cheng.data.entity.UserInfoEntity;
import com.cheng.data.mapper.UserInfoMapper;
import com.cheng.data.vo.PermInfoVO;
import com.cheng.data.vo.RoleInfoVO;
import com.cheng.data.vo.UserInfoVO;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author: ChengJW
 * @date: 2021/11/2 7:16
 * @version: 1.0
 * @description: 自定义的 realm 用于身份认证、授权
 */
public class CustomRealm extends AuthorizingRealm {

    private final static Logger log = LoggerFactory.getLogger(CustomRealm.class);

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
    * @author: ChengJW
    * @params: [authenticationToken]
    * @return: org.apache.shiro.authc.AuthenticationInfo
    * @date: 2021/11/2
    * @description: 身份认证
    */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        try {
            if (null == authenticationToken){
                log.error("authenticationToken为空!");
                throw new CommonException("CJW202111010018","CustomRealm","doGetAuthenticationInfo");
            }
            UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
            UserInfoEntity userInfoEntity = new UserInfoEntity();
            userInfoEntity.setUrNm(token.getUsername());
            UserInfoVO userInfoVO = userInfoMapper.queryBaseUserInfo(userInfoEntity);
            if (userInfoVO != null){
               return new SimpleAuthenticationInfo(userInfoVO,userInfoVO.getUrPwd(),getName());
            }
            return null;
        } catch (Exception exception) {
            log.error("CustomRealm-doGetAuthenticationInfo");
            log.error(exception.getMessage(),exception);
            throw new CommonException(null,"CustomRealm","doGetAuthenticationInfo",exception);
        }
    }

    /**
    * @author: ChengJW
    * @params: [principalCollection]
    * @return: org.apache.shiro.authz.AuthorizationInfo
    * @date: 2021/11/2
    * @description: 权限管理
    */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        try {
            if (null == principalCollection) {
                log.error("CustomRealm-doGetAuthorizationInfo异常:principalCollection is null");
                throw new CommonException("CJW202111010019","CustomRealm","doGetAuthorizationInfo");
            }
            UserInfoVO userInfoVO = (UserInfoVO)principalCollection.getPrimaryPrincipal();
            RoleInfoVO roleInfoVO = userInfoVO.getRoleInfoVO();
            List<PermInfoVO> permInfoVOList = userInfoVO.getPermInfoVOList();

            SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
            if (null != roleInfoVO){
                authorizationInfo.addRole(roleInfoVO.getRoNm());
            }
            if (null != permInfoVOList && permInfoVOList.size() > 0){
                for (PermInfoVO entity : permInfoVOList){
                    authorizationInfo.addStringPermission(entity.getPrNm());
                }
            }
            return authorizationInfo;
        } catch (Exception exception) {
            log.error("CustomRealm-doGetAuthorizationInfo");
            log.error(exception.getMessage(),exception);
            throw new CommonException(null,"CustomRealm","doGetAuthorizationInfo",exception);
        }
    }
}
